package main

import (
	"database/sql"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"strings"
	"time"

	"log"

	_ "github.com/go-sql-driver/mysql"
)

func out(w http.ResponseWriter, text string) {
	w.Write([]byte(string))
}

func hello(w http.ResponseWriter, req *http.Request) {
}
func say(w http.ResponseWriter, req *http.Request) {
	//	w.Write([]byte("Hello"))
	//insert update query delete
	biz := req.FormValue("biz")
	param := req.FormValue("param")

	sqlstrs := readFile(biz)

	res := ""

	if strings.Contains(sqlstrs, biz) {
		start := strings.Index(sqlstrs, biz) + len(biz) + 2
		sqlstr := sqlstrs[start]
		end := strings.Index(sqlstr, "[")
		if end > 0 {
			end := strings.Index(sqlstr, "[")
			res := sqlstr[0:end]
		} else {
			res = sqlstr
		}
		fmt.Println(res)
	} else {
		out(w, "无"+biz+"对应sql")
	}

}

// 获取db
func getDb() *sql.DB {
	db, err := sql.Open("mysql", "root:123456@tcp(192.168.0.27:3306)/network_testcenter_20170516")
	printError(err)
	return db
}

func query(sqlstr string, arr []) string {

}

func readFile(name string) string {
	file, err := os.Open(name)
	if err != nil {
		panic(err)
	}
	txt, err := ioutil.ReadAll(file)
	if err != nil {
		panic(err)
	}
	return string(txt)
}

func main() {
	http.HandleFunc("/hello", hello)
	http.Handle("/handle", http.HandlerFunc(say))
	http.ListenAndServe(":8001", nil)
	select {} //阻塞进程
}
